package com.yuniarti.konsultasikesehatan;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.ListActivity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class InformasiActivity extends ListActivity {

		DataParser jParser = new DataParser();
	 
	    ArrayList<HashMap<String, String>> artikelList;
	 
	    private static String url_informasi = "http://konsultasi-yakes.web.id/JSON/artikel-json.php";
	    //private static String url_informasi = "http://10.0.2.2/adminweb/JSON/artikel-json.php";
	    
	    private static final String AR_ARTIKEL = "artikel";
	    private static final String AR_ID = "id";
	    private static final String AR_JUDUL = "judul";
	 
	    JSONArray artikel = null;
	    
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_informasi);
		
		artikelList = new ArrayList<HashMap<String, String>>();
		 
        new LoadArtikel().execute();
 
        ListView lv = getListView();

        lv.setOnItemClickListener(new OnItemClickListener() {
 
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long xid) {
           
                String id = ((TextView) view.findViewById(R.id.kode)).getText().toString();
 
                Intent in = new Intent(getApplicationContext(),DetailInfoActivity.class);
                in.putExtra(AR_ID, id);
                startActivity(in);
            }
        });
 
    }
 
    class LoadArtikel extends AsyncTask<String, String, String> {
 
        protected String doInBackground(String... args) {

            List<NameValuePair> params = new ArrayList<NameValuePair>();

            JSONObject json = jParser.makeHttpRequest(url_informasi, "GET", params);
 
            try {
        
                    artikel = json.getJSONArray(AR_ARTIKEL);

                    for (int i = 0; i < artikel.length(); i++) {
                    	
                        JSONObject c = artikel.getJSONObject(i);
 
                        String id = c.getString(AR_ID);
                        String judul = c.getString(AR_JUDUL);
 
                        HashMap<String, String> map = new HashMap<String, String>();
 
                        map.put(AR_ID, id);
                        map.put(AR_JUDUL, judul);
 
                        artikelList.add(map);
                    }
          
            } catch (JSONException e) {
                e.printStackTrace();
            }
 
            return null;
        }
 
        protected void onPostExecute(String file_url) {
   
            runOnUiThread(new Runnable() {
                public void run() {
                 
                    ListAdapter adapter = new SimpleAdapter(
                            InformasiActivity.this, artikelList,
                            R.layout.list_item, new String[] { AR_ID, AR_JUDUL},
                            new int[] { R.id.kode, R.id.judul });

                    setListAdapter(adapter);
                }
            });
 
        }
 
    }
}
